package org.hit.burkun.cases.mapping;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.hit.burkun.db.DBTypeConverter.DATATYPE;
import org.hit.burkun.dbimport.MemoryImport;
import org.hit.burkun.entities.Entity;
import org.hit.burkun.entities.Entity.TYPE;
import org.hit.burkun.file.FileProcessHelper;
import org.hit.burkun.file.FileProcessHelper.FILE_TYPE;
import org.hit.burkun.file.IDataLineProcesser;
import org.hit.burkun.umls.GeneralQuery;
import org.hit.burkun.util.GlobalData;

public class OmimMesh2DoMapping {
	// 使用程亮师兄的mapping程序
	public static void main(String[] args) {
		Collection<String[]> data = getMappingData();
		DATATYPE types[] = new DATATYPE[9];
		types[0] = DATATYPE.I;
		DBTypeConverter typeConverter = new DBTypeConverter(types);
		MemoryImport mi = new MemoryImport("do_mapping",
				DBHelper.getLocalBioSearch(), true, typeConverter);
		mi.doImport(data);
	}

	public static Collection<String[]> getMappingData() {
		String path1 = GlobalData.getInstance().getOmim2DoPath();
		String path2 = GlobalData.getInstance().getMesh2DoPath();
		LinkedList<String[]> res1 = getResByFile(path1, Entity.TYPE.OMIM);
		LinkedList<String[]> res2 = getResByFile(path2, Entity.TYPE.MSH);
		res1.addAll(res2);
		return res1;
	}

	private static LinkedList<String[]> getResByFile(String path,
			final TYPE type) {
		FileProcessHelper fph = new FileProcessHelper(path);
		final LinkedList<String[]> res = new LinkedList<String[]>();
		final LinkedList<String> ids = new LinkedList<String>();
		final LinkedList<String> doids = new LinkedList<String>();
		fph.process(new IDataLineProcesser() {
			private int beginLine = 2;
			private int counter = 1;

			@Override
			public String[] splitLine(String line) {
				return tabCsv(line);
			}

			@Override
			public String[] processItems(String[] items) {
				String[] newItem = new String[items.length + 3];
				newItem[0] = items[0];
				newItem[2] = items[1];
				newItem[3] = type.getTag();
				newItem[4] = items[2];
				ids.add(newItem[4]);
				doids.add(newItem[0]);
				newItem[6] = items[3];
				newItem[7] = items[4];
				return newItem;
			}

			@Override
			public void doPostOutside(String[] items) {
				res.add(items);
			}

			@Override
			public void cleanUp() {
				// do nothing
			}

			@Override
			public String chooseLine(String line) {
				if (counter < beginLine) {
					counter++;
					return null;
				}
				return line;
			}
		}, FILE_TYPE.PLAIN);
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids");
		gq.begin();
		gq.quickMap(ids, type);
		gq.quickMap(doids, Entity.TYPE.DISEASE_ONTOLOGY);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		LinkedList<String[]> finalRes = new LinkedList<String[]>();
		for (String[] line : res) {
			if (mapRes.containsKey(line[4]) && mapRes.containsKey(line[0])) {
				Set<String> umls = mapRes.get(line[4]);
				Set<String> doUmls = mapRes.get(line[0]);
				line[5] = StringUtils.join(umls, ",");
				line[1] = StringUtils.join(doUmls, ",");
				finalRes.add(line);
			}
		}
		return finalRes;
	}

}
